package edu.bu.cs673.superkids123.children.model;
import javax.swing.ImageIcon;

import edu.bu.cs673.superkids123.children.model.resources.ChildrenResource;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
//Is this OK? or bad practice?  -Decided it was not very significant but better to add this logic to the model instead of the controller. T
//This is the same practice used in Rails
//My model and the question generator are combined into 1
public class MatchQuestion {

	private ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
	private int correctAnswer;
	private int[] options;
	private int correctAnswerLocation;
	
	public MatchQuestion(int age, int numOptions){
		int range;
		options = new int[numOptions+1];
		this.correctAnswerLocation = 1 + (int) ( Math.random() * (numOptions) );
		if ( age == 2 )
			range = 6;
		else if ( age == 3 )
			range = 10;
		else if ( age == 4 )
			range = 16;
		else if ( age == 5 )
			range =  20;
		else 
			range = 20;

		this.correctAnswer = (int) (Math.random() * (range+1) );
		for(int i=1;i<=numOptions; i++){
			if(i == correctAnswerLocation) options[i] = correctAnswer;
			else{ //find an option 1-10  must but not the correctAnswer and no duplicates
				boolean invalid = true;
				int option = -1;
				while(invalid){
					option = (int) (Math.random() * (range+1) );
					invalid = false;
					if(option == correctAnswer) invalid =true;
					for(int j=1;j<i;j++)
						if(options[j]==option)	invalid=true; 
				}
				options[i] = option;
			}
		}
		
		
	}
	public ImageIcon atAnswerOption(int location) {
		return provider.getScaledImage(ChildrenResource.NUMBER_IMAGE_MAP.get(options[location]), (float) 0.5);
	}
	public ImageIcon getAnswerImage() {
		return provider.getScaledImage(ChildrenResource.NUMBER_IMAGE_MAP.get(correctAnswer), (float) 0.5);
	}
	public int getCorrectAnswerLocation(){
		return correctAnswerLocation;
	}
	public int getOption(int optionLocation){
		return options[optionLocation];
	}
	public int getAnswer(){
		return correctAnswer;
	}
}
